#include "Sock.hpp"
#include<poll.h>

#define NUM 128

struct pollfd fd_array[NUM]; //内容大于0，合法的fd，如果是-1,该位置没有fd

int main(int argc, char* argv[])
{
    if(argc != 2)
    {
        cout << "Usage:\n\t" << "port" << endl;
        exit(1);
    }

    int listen_sock = sock::Socket();
    sock::Bind(listen_sock, atoi(argv[1]));
    sock::Listen(listen_sock);
    for(int i = 0; i < NUM; i++)
    {
        fd_array[i].fd = -1;
        fd_array[i].events = 0;
        fd_array[i].revents = 0;
    }
    // //事件循环
    fd_array[0].fd = listen_sock;
    fd_array[0].events = POLLIN;
    fd_array[0].revents = 0;
    while(1)
    {
        //recv,read,write,send,accept:只负责自己最核心的工作，真正的读写（listen_sock:accept）
        int n = poll(fd_array, NUM, -1);
        switch(n)
        {
            case -1:
                cerr << "poll error" << endl;
                break;
            case 0:
                cerr << "poll timeout" << endl;
                break;
            default:
                cout << "有fd对应的事件就绪了!" << endl;
                for(int i = 0; i < NUM; i++)
                {
                    if(fd_array[i].fd == -1)
                    {
                        continue;
                    }
                    if(POLLIN & fd_array[i].revents)
                    {
                        cout << "sock: " << fd_array[i].fd << " 上面有了读事件，可以读取了" << endl;
                        if(fd_array[i].fd == listen_sock)
                        {
                            cout << "listen_sock: " << listen_sock << "有了新的连接到来" << endl;
                            //accept
                            int sock = sock::Accept(listen_sock);
                            if(sock >= 0)
                            {
                                cout << "listen_sock: " << listen_sock << "获取新的连接成功" << endl;
                                int pos = 1;
                                for(; pos < NUM; pos++)
                                {
                                    if(fd_array[pos].fd == -1)
                                    {
                                        break;
                                    }
                                }
                                if(pos < NUM)
                                {
                                    cout << "新连接: " << sock << "已经添加到了数组[" << pos << "]的位置" << endl;
                                    fd_array[pos].fd = sock;
                                    fd_array[pos].events = POLLIN;
                                }
                                else
                                {
                                    //说明服务器已经满载，没法处理新的请求
                                    cout << "服务器已经满载了，关闭新的连接" << endl;
                                    close(sock);
                                }
                            }
                        }                       
                        else
                        {
                            //read,recv
                            //注意读取的时候也要构建协议，比如怎么读取避免粘包问题，如果一次读不完，一次该读多少数据。
                            cout << "sock: " << fd_array[i].fd << "上面有普通数据读取" << endl;
                            char recv_buffer[1024] = {0};
                            ssize_t s = recv(fd_array[i].fd, recv_buffer, sizeof(recv_buffer) - 1, 0);
                            if(s > 0)
                            {
                                recv_buffer[s] = '\0';
                                cout << "client[ " << fd_array[i].fd << "]# " << recv_buffer << endl;
                            }
                            else if(s == 0)
                            {
                                cout << "sock: " << fd_array[i].fd << "关闭了，client退出了" << endl;
                                //对端关闭了连接
                                close(fd_array[i].fd);
                                

                                cout << "已经在数组下标fd_array[" << i << "]中去掉了sock: " << fd_array[i].fd << endl;
                                fd_array[i].fd = -1;
                            }
                            else
                            {
                                //读取失败
                                close(fd_array[i].fd);
                                

                                cout << "已经在数组下标fd_array[" << i << "]中去掉了sock: " << fd_array[i].fd << endl;
                                fd_array[i].fd = -1;
                            }
                        }
                    }
                }
                break;
        }
    }

    return 0;
}
